package com.lanou.tipsy.fragment.search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.search.SearchInfoAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.search.SearchSongEntity;
import com.lanou.tipsy.entity.search.SongSearch;
import com.lanou.tipsy.tools.formatCodeUtil.FormatCodeUtil;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by ws on 15/10/27.
 */
public class SearchInfoFragment extends BaseFragment {


    private ListView searchInfoLV;
    private SearchInfoAdapter adapter;
    private ArrayList<SearchSongEntity> data;
    private VolleySingleton volleySingleton;//单例
    private ImageView SearchInfoBackIV;//返回按钮
    private FragmentManager manager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fr_search_info, null);

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init(getView());

    }

    private void init(View view) {
        searchInfoLV = (ListView) view.findViewById(R.id.searchInfoLV);
        SearchInfoBackIV = (ImageView) view.findViewById(R.id.SearchInfoBackIV);
        //监听
        SearchInfoBackIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();

            }
        });
        Bundle bundle = getArguments();
        //utf-8格式码转换
        String s = bundle.getString("type");
        String song = FormatCodeUtil.codingFormat(s);
        String url = Constants.SEARCH_URL+
                song;
        //初始化
        volleySingleton = VolleySingleton.getInstance();
        StringRequest sr = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongSearch songSearch = gson.fromJson(response, SongSearch.class);
                data = songSearch.getSong();
                adapter = new SearchInfoAdapter(getContext(), data);
                searchInfoLV.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


            }
        });
        volleySingleton.addQueue(sr);
    }
}
